package com.ruoyi.task.service.impl;

import com.ruoyi.common.core.domain.dto.TaskReviewDto;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.system.service.IFileService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.task.domain.Task;
import com.ruoyi.task.mapper.TaskCommentMapper;
import com.ruoyi.task.mapper.TaskMapper;
import com.ruoyi.task.service.ITaskService;
import com.ruoyi.wechat.domain.WechatUser;
import com.ruoyi.wechat.service.IWechatUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 任务Service业务层处理
 *
 * @author KevenPotter
 * @date 2024-04-28 16:51:27
 */
@Service
@AllArgsConstructor
public class TaskServiceImpl implements ITaskService {

    private final ISysUserService userService;
    private final IWechatUserService wechatUserService;
    private final IFileService fileService;
    private final TaskMapper taskMapper;
    private final TaskCommentMapper taskCommentMapper;

    /**
     * 查询任务
     *
     * @param id 任务主键
     * @return 任务
     */
    @Override
    public Task getById(Long id) {
        return taskMapper.getById(id);
    }

    /**
     * 查询任务列表
     *
     * @param task 任务
     * @return 任务
     */
    @Override
    public List<Task> getList(Task task) {
        return taskMapper.getList(task);
    }

    /**
     * 查询所有任务
     *
     * @return 返回所有任务
     * @author KevenPotter
     * @date 2024-04-29 15:41:38
     */
    @Override
    public List<Task> getAllList() {
        return taskMapper.getAllList();
    }

    /**
     * 新增任务
     *
     * @param task 任务
     * @return 结果
     */
    @Override
    public int insertTask(Task task) {
        task.setCreateTime(DateUtils.getNowDate());
        return taskMapper.insertTask(task);
    }

    /**
     * 修改任务
     *
     * @param task 任务
     * @return 结果
     */
    @Override
    public int updateTask(Task task) {
        task.setUpdateTime(DateUtils.getNowDate());
        return taskMapper.updateTask(task);
    }

    /**
     * 批量删除任务
     *
     * @param ids 需要删除的任务主键
     * @return 结果
     */
    @Override
    public int deleteTaskByIds(Long[] ids) {
        return taskMapper.deleteTaskByIds(ids);
    }

    /**
     * 删除任务信息
     *
     * @param id 任务主键
     * @return 结果
     */
    @Override
    public int deleteTaskById(Long id) {
        return taskMapper.deleteTaskById(id);
    }

    /**
     * 审核任务
     *
     * @param taskReviewDto 任务审核数据传输
     * @return 返回是否审核成功
     * @author KevenPotter
     * @date 2024-04-29 10:09:09
     */
    @Override
    public int review(TaskReviewDto taskReviewDto) {
        Integer reviewStatus = taskReviewDto.getReviewStatus();
        if (Objects.equals(reviewStatus, 98)) {
            return taskMapper.reviewReject(taskReviewDto);
        } else if (Objects.equals(reviewStatus, 99)) {
            return taskMapper.reviewPass(taskReviewDto);
        }
        return 0;
    }

    /**
     * 标记精选操作
     *
     * @param taskId 任务编号
     * @return 返回标记精选是否成功
     * @author KevenPotter
     * @date 2024-05-01 10:06:06
     */
    @Override
    public int markFeatured(Long taskId) {
        return taskMapper.markFeatured(taskId);
    }

    /**
     * 取消精选操作
     *
     * @param taskId 任务编号
     * @return 返回取消精选是否成功
     * @author KevenPotter
     * @date 2024-05-01 10:06:12
     */
    @Override
    public int cancelFeatured(Long taskId) {
        return taskMapper.cancelFeatured(taskId);
    }

    /**
     * 标记置顶操作
     *
     * @param taskId 任务编号
     * @return 返回标记置顶是否成功
     * @author KevenPotter
     * @date 2024-05-01 10:46:25
     */
    @Override
    public int markTop(Long taskId) {
        return taskMapper.markTop(taskId);
    }

    /**
     * 取消置顶操作
     *
     * @param taskId 任务编号
     * @return 返回取消置顶是否成功
     * @author KevenPotter
     * @date 2024-05-01 10:46:39
     */
    @Override
    public int cancelTop(Long taskId) {
        return taskMapper.cancelTop(taskId);
    }

    /**
     * 查询任务总数
     *
     * @return 返回任务总数
     * @author KevenPotter
     * @date 2024-05-01 20:08:13
     */
    @Override
    public int getTaskCounts() {
        return taskMapper.getTaskCounts();
    }

    /**
     * 查询精选任务总数
     *
     * @return 返回精选任务总数
     * @author KevenPotter
     * @date 2024-05-01 20:08:18
     */
    @Override
    public int getTaskFeaturedCounts() {
        return taskMapper.getTaskFeaturedCounts();
    }

    /**
     * 过滤出埠参数参数
     *
     * @param task      任务对象
     * @param loginUser 当前登录对象
     * @return 返回过滤后的[任务对象]
     * @author KevenPotter
     * @date 2024-04-28 23:53:18
     */
    @Override
    public Task filterInbound(Task task, LoginUser loginUser) {
        if (StringUtils.isNotEmpty(task.getTaskImage())) task.setTaskImage(FileUtils.buildFilesUrl(task.getTaskImage().split(SPLIT)));
        return task;
    }

    /**
     * 过滤出埠参数参数
     *
     * @param task      任务对象
     * @param loginUser 当前登录对象
     * @return 返回过滤后的[任务对象]
     * @author KevenPotter
     * @date 2024-04-28 23:53:29
     */
    @Override
    public Task filterOutbound(Task task, LoginUser loginUser) {
        if (StringUtils.isNotEmpty(task.getTaskImage())) {
            String taskImage = task.getTaskImage();
            task.setTaskImage(String.join(SPLIT, fileService.buildMultipleFileUrlsFullPath(taskImage)));
            task.setTaskImageList(fileService.buildMultipleFileUrlsFullPath(taskImage));
        }
        if (task.getPublishUserId() != null) {
            WechatUser publishUser = wechatUserService.getById(task.getPublishUserId());
            task.setPublishUserName(publishUser.getNickName()).setAvatar(publishUser.getAvatar().contains("https") ? publishUser.getAvatar() : fileService.buildSingleFileUrlFullPath(publishUser.getAvatar()));
        }
        if (task.getReviewUserId() != null) task.setReviewUserName(userService.selectUserById(task.getReviewUserId()).getNickName());
        task.setCommentCounts(taskCommentMapper.getCommentCountsByTaskId(task.getId()));
        return task;
    }
}
